package com.itheima.wemedia.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.dto.WmMaterialPageRequestDto;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.service.WmMaterialService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import com.itheima.core.controller.AbstractCoreController;

/**
 * @description <p>自媒体图文素材信息</p>
 *
 * @version 1.0
 * @package com.itheima.wemedia.controller
 */
@Api(value="WmMaterialController",tags = "自媒体图文素材信息")
@RestController
@RequestMapping("/material")
public class WmMaterialController extends AbstractCoreController<WmMaterial> {

    private WmMaterialService wmMaterialService;

    @Autowired
    public WmMaterialController(WmMaterialService wmMaterialService) {
        super(wmMaterialService);
        this.wmMaterialService=wmMaterialService;
    }

    /**
     * 素材分页查询
     * @param wmMaterialPageRequestDto 接收分页参数
     * @return 返回分页查询数据
     */
    @ApiOperation("素材分页查询")
    @PostMapping("/list")
    public PageResultVo findPage(@RequestBody WmMaterialPageRequestDto wmMaterialPageRequestDto){
        //调用service层的方法进行分页查询
        PageResultVo pageResultVo = wmMaterialService.findPage(wmMaterialPageRequestDto);
        return pageResultVo;
    }

    /**
     * 添加素材收藏接口，更新素材表
     * @param id 素材id
     * @return 返回是否跟新成功
     */
    @ApiOperation("添加素材收藏")
    @GetMapping("/collect/{id}")
    public ResultVo update(@PathVariable Long id){
        //调用service测的方法机进行进行更新操作
        if (wmMaterialService.updateIsCollect(id)){
            return ResultVo.ok("收藏素材成功");
        }
        return ResultVo.bizError("素材收藏失败");
    }

    /**
     * 素材删除接口
     * @param id 素材id
     * @return 返回是否删除成功
     */
    @ApiOperation("素材删除接口")
    @GetMapping("/del_picture/{id}")
    public ResultVo del(@PathVariable Long id){
        if (wmMaterialService.deleteMaterial(id)){
            return ResultVo.ok("删除素材成功");
        }
        return ResultVo.bizError("删除素材失败！");
    }
    @GetMapping("/cancel_collect/{id}")
    public ResultVo updateCollect(@PathVariable Long id){
        if (wmMaterialService.updateIsCollect(id)) {
            return ResultVo.ok("取消收藏成功！");
        }
        return ResultVo.bizError("取消收藏失败！");
    }
}

